


from ast import parse
import pdb
from tkinter import Radiobutton
from PIL import Image 


import argparse
import pdb 

import numpy as np
from torchvision.transforms import Resize


from os.path import join, split, exists, isdir,isfile

if __name__=='__main__':



    parser = argparse.ArgumentParser()

    parser.add_argument('--src', type=str, default= 'example_images/M3_Open_World_equirectangular-jpg_overgrown_crumbling_castle_ruins_961606579_12755606.jpg', help=' original image', )

    parser.add_argument('--o', type=str, default='example_images', help= ' output path ')
    parser.add_argument('--max_length', type=int, default=1280, help= ' output path ')

    args = parser.parse_args()

    root = '/baai-cwm-1/baai_cwm_ml/algorithm/shaocong.xu/exp/MoGe'

    src_img = Image.open(join(root, args.src))

    W, H= src_img.size #* Pillow image is [W,H], opencv image is [H,W]
    # pdb.set_trace()

    print('origin image size: {}'.format(src_img.size))

    
    """
    W  * ratio = 1024
    ratio = 1024 / W
    
    
    """


    # resizer = Resize()


    print('args.max_length',args.max_length)
    ratio = args.max_length / W

    tgt_img = src_img.resize((int(W *ratio), int(H *ratio) ))
    
    
    print(f'ratio : %f \t new image size: {tgt_img.size}'%(ratio ) )

    # pdb.set_trace()

    assert args.src.endswith('.jpg'), 'it is not jpg formate '


    tgt_path = join(root, args.src.replace('.jpg', f"_{'-'.join(np.array(tgt_img.size).astype(np.str0).tolist())}.jpg"))

    

    tgt_img.save(tgt_path)
    




